<?php
    $post = $_POST;
    $unit = arrGet($post,"unit","kg");
    unset($post["unit"]);
    
    $data = API::workoutOneRepMax($post);
    
    # Yksikkö on tuntematon
    if (!in_array($unit,array("kg","lb"))) {
        $unit = "kg";
    }
?>

<script id="onerepmax-chartdata" type="application/json">
    <?php echo htmlescape(json_encode($data)); ?>
</script>

<script id="onerepmax-unit" type="text/plain">
    <?php echo htmlescape($unit); ?>
</script>

<table class="table table-hover">
    <thead>
        <tr>
            <th>
                <button type="button" class="btn btn-default btn-sm" id="onerepmax-button-open-chart" title="Chart">
                    <span class="glyphicon glyphicon-signal" aria-hidden="true"></span>
                </button>
            </th>
            <th>
                #                
            </th>
            <th>
                Date
            </th>
            <th>
                Exercise
            </th>
            <th>
                Rep type
            </th>
            <th>
                Reps
            </th>
            <th>
                Weight&nbsp;(<?php echo mb_strtoupper($unit); ?>)
            </th>
            <th>
                1RM&nbsp;(<?php echo mb_strtoupper($unit); ?>)
            </th>
            <th>
                Calculated
            </th>
        </tr>
    </thead>
    
    <tbody>
        <?php
            $i = 1;
            foreach ($data as $row) {
            ?>
                <tr>
                    <td>
                        &nbsp;
                    </td>
                    <td>
                        <?php echo $i; ?>
                    </td>
                    <td>
                        <a class="a-subview-dialog"
                          data-ui-view="list"
                          data-ui-subview="info"
                          data-ui-title="<?php echo htmlescape($row["extratext"]); ?>"
                          data-hashid="<?php echo htmlescape($row["hashid"]); ?>"
                          href="#">
                            <?php echo htmlescape($row["startdate"]); ?>
                        </a>
                    </td>
                    <td>
                        <?php
                            # Onko levytangotyyppiä asetettu
                            $barbellTypeDesc = "";
                            if ($row["barbell_type"]) {
                                $barbellTypeDesc = "&nbsp;(".
                                    htmlescape(getBarbellTypeDesc($row["barbell_type"])).
                                    ")";
                            }
                            echo htmlescape($row["exercisetext"]).
                                $barbellTypeDesc;
                        ?>
                    </td>
                    <td>
                        <?php
                            echo htmlescape(
                                getRepetitionsTypeDesc($row["repetitions_type"])
                            );
                        ?>
                    </td>
                    <td>
                        <?php echo htmlescape($row["repetitions"]); ?>
                    </td>
                    <td>
                        <?php echo htmlescape($row["weight_$unit"]); ?>
                    </td>
                    <td>
                        <?php echo htmlescape($row["calculated_$unit"]); ?>
                    </td>
                    <td>
                        <?php echo (($row["calculated"]) ? "Yes" : "No"); ?>
                    </td>
                </tr>
            <?php
                $i++;
            }
        ?>
    </tbody>
</table>